﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using System.Web.UI.HtmlControls;
using Yipusoft.ProfessionalTeam.BLL;
using Yipusoft.ProfessionalTeam.DAL;

public partial class MapInfo_ProfessionalTeam_HelpStat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDepartment();
            BindTable();
        }
    }

    void BindDepartment()
    {
        string where = " DepartmentId like '%000' and DepartmentId <>'1300000' ";
        List<DepartmentInfo> list = Department.GetList(where);
        DepartmentList.DataSource = list;
        DepartmentList.DataBind();
        if (Request["DepartmentId"] != null)
        {
            DepartmentList.SelectedValue = Request["DepartmentId"];
        }
    }

    void BindTable()
    {
        int HelpCount = 0;
        int procsCount = 0;
        int assaultCount = 0;
        string ParentId = DepartmentList.SelectedValue;
        string where = " DepartmentId like '" + ParentId.Substring(0, 4) + "3%'";
        List<DepartmentInfo> list = Department.GetList(where);
        //头
        HtmlTableRow tr = new HtmlTableRow();
        HtmlTableCell td = new HtmlTableCell();
        td.InnerText = "单位";

        tr.Cells.Add(td);
        td = new HtmlTableCell();
        td.InnerText = "专业队伍名称";
        td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
        tr.Cells.Add(td);
        td = new HtmlTableCell();
        td.InnerText = "是否达标";
        td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
        tr.Cells.Add(td);
        td = new HtmlTableCell();
        td.InnerText = "更新时间";
        td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
        tr.Cells.Add(td);

        table.Rows.Add(tr);
        TeamDataContext tdc = new TeamDataContext();
        //主体
        for (int i = 0; i < list.Count; i++)
        {//部门
            string DepartmentId = list[i].DepartmentID;
            tr = new HtmlTableRow();
            HtmlTableCell departmentTd = new HtmlTableCell();
            departmentTd.InnerText = list[i].ShortName;
            tr.Cells.Add(departmentTd); string DepartmentName = Department.GetDepartment(DepartmentId).FullName;
            int DepartmentRowSpan = 0;
            if (!DepartmentName.Contains("特勤"))
            {
                //-------------抢险救援班------------------------------------------------------------
                departmentTd.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                HtmlTableCell teamtd = new HtmlTableCell();
                teamtd.InnerText = "抢险救援班";
                teamtd.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(teamtd);
                HtmlTableCell isStandardTd = new HtmlTableCell();



                if (Yipusoft.ProfessionalTeam.BLL.Stat.isComplianceHelpByDepartment(DepartmentId))
                {
                    HelpCount++;
                    isStandardTd.InnerHtml = "<a href='HelpNew.aspx?DepartmentID=" + DepartmentId + "&Type=0'>√</a>";
                }
                else
                {
                    isStandardTd.InnerHtml = "<a href='HelpNew.aspx?DepartmentID=" + DepartmentId + "&Type=0'>×</a>";
                }



                isStandardTd.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(isStandardTd);
                HtmlTableCell UpdateDateTd = new HtmlTableCell();
                UpdateDateTd.InnerHtml = Yipusoft.ProfessionalTeam.BLL.Stat.GetHelpLastUpdateTime(DepartmentId) == DateTime.MinValue ? "" : Yipusoft.ProfessionalTeam.BLL.Stat.GetHelpLastUpdateTime(DepartmentId).ToShortDateString();
                UpdateDateTd.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(UpdateDateTd);
                table.Rows.Add(tr);
                DepartmentRowSpan++;
            }
            else
            {
                //-------------抢险救援班------------------------------------------------------------
                departmentTd.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                HtmlTableCell teamtd = new HtmlTableCell();
                teamtd.InnerText = "----";
                teamtd.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(teamtd);
                HtmlTableCell isStandardTd = new HtmlTableCell();
                isStandardTd.InnerHtml = "----";
                isStandardTd.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(isStandardTd);
                HtmlTableCell UpdateDateTd = new HtmlTableCell();
                UpdateDateTd.InnerHtml = "----";
                UpdateDateTd.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(UpdateDateTd);
                table.Rows.Add(tr);
                DepartmentRowSpan++;
            }
            //---------------专业队伍--------------------------------------------------------------
            List<ProfessionalProc> procs = tdc.ProfessionalProc.Where(o => o.DepartmentId == DepartmentId).ToList();

            for (int j = 0; j < procs.Count; j++)
            {
                if (DepartmentRowSpan != 0)
                {
                    tr = new HtmlTableRow();
                }
                HtmlTableCell procCell = new HtmlTableCell();
                procCell.InnerText = tdc.ProcsType.Where(o => o.Id == procs[j].ProcessionType).FirstOrDefault().TypeName;
                procCell.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(procCell);
                procCell = new HtmlTableCell();
                procCell.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                if (Yipusoft.ProfessionalTeam.BLL.Stat.isComplianceProsByPid(procs[j].id))
                {
                    procsCount++;
                    procCell.InnerHtml = "<a href=HelpNew.aspx?DepartmentID=" + DepartmentId + "&Pid=" + procs[j].id + "&Type=1>√</a>";
                }
                else
                {
                    procCell.InnerHtml = "<a href=HelpNew.aspx?DepartmentID=" + DepartmentId + "&Pid=" + procs[j].id + "&Type=1>×</a>";
                }

                tr.Cells.Add(procCell);
                procCell = new HtmlTableCell();
                procCell.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                DateTime LastUpdateDate = Yipusoft.ProfessionalTeam.BLL.Stat.GetProsLastUpdateDateByPid(procs[j].id);
                procCell.InnerText = LastUpdateDate == DateTime.MinValue ? "" : LastUpdateDate.ToShortDateString();
                tr.Cells.Add(procCell);
                DepartmentRowSpan++;
                table.Rows.Add(tr);
            }
            //--------------------攻坚组---------------------------------------------------
            List<Assault> assaults = tdc.Assault.Where(o => o.DepartmentId == DepartmentId).ToList();
            for (int j = 0; j < assaults.Count; j++)
            {
                tr = new HtmlTableRow();
                HtmlTableCell procCell = new HtmlTableCell();
                procCell.InnerText = tdc.AssaultType.Where(o => o.Id == assaults[j].TypeId).FirstOrDefault().TypeName;
                procCell.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(procCell);
                procCell = new HtmlTableCell();
                procCell.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                if (Yipusoft.ProfessionalTeam.BLL.Stat.isComplianceAssaultByPid(assaults[j].Id))
                {
                    assaultCount++;
                    procCell.InnerHtml = "<a href=HelpNew.aspx?DepartmentID=" + DepartmentId + "&Aid=" + assaults[j].Id + "&Type=2>√</a>";
                }
                else
                {
                    procCell.InnerHtml = "<a href=HelpNew.aspx?DepartmentID=" + DepartmentId + "&Aid=" + assaults[j].Id + "&Type=2>×</a>";
                }
                tr.Cells.Add(procCell);
                procCell = new HtmlTableCell();
                procCell.Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                DateTime LastUpdateDate = Yipusoft.ProfessionalTeam.BLL.Stat.GetAssaultLastUpdateDate(assaults[j].Id);
                procCell.InnerText = LastUpdateDate == DateTime.MinValue ? "" : LastUpdateDate.ToShortDateString();
                tr.Cells.Add(procCell);
                DepartmentRowSpan++;
                table.Rows.Add(tr);
            }

            departmentTd.RowSpan = DepartmentRowSpan;
            lblAssaultCount.Text = assaultCount.ToString();
            lblHelpCount.Text = HelpCount.ToString();
            lblProcsCount.Text = procsCount.ToString();
        }

    }


    protected void DepartmentList_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("HelpStat.aspx?DepartmentId=" + DepartmentList.SelectedValue);
    }
}
